package com.lysj.admin.master.upload.controller;

import cn.hutool.core.util.IdUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.StorageClass;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.spring.base.BaseContent;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import com.lysj.admin.master.upload.domain.UploadInfo;
import com.lysj.admin.master.upload.service.UploadService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/upload")
public class UploadController extends BaseContent {

    @Resource
    private UploadService uploadService;

//    @PostMapping("/files_upload")
//    public Resp filesUpload(MultipartFile file) {
//        // Endpoint以杭州为例，其它Region请按实际情况填写。
//        String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
//// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
//        String accessKeyId = "LTAI4Foj57mFVDfnEnxfdcmc";
//        String accessKeySecret = "kaHQcIRDkOaUOk2TukGJ8SnesvKBAn";
//        String bucketName = "lysj-jiaoyu";
//// 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        InputStream inputStream = null;
//        try {
//            String originalName = file.getOriginalFilename();
//            String suffix = originalName.substring(originalName.lastIndexOf("."));
//            String newName = IdUtil.fastSimpleUUID() + suffix;
//            String objectName = CommonConstant.PROJECT_NAME + "/" + newName;
//            inputStream = file.getInputStream();
//            // 创建PutObjectRequest对象。
//            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
////            ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
//// 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
//            ObjectMetadata metadata = new ObjectMetadata();
//            metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
//            metadata.setObjectAcl(CannedAccessControlList.PublicReadWrite);
//            putObjectRequest.setMetadata(metadata);
//
//// 上传文件。
//            ossClient.putObject(putObjectRequest);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (inputStream != null) {
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                }
//            }
//            // 关闭OSSClient。
//            ossClient.shutdown();
//        }
//        return RespUtil.success("上传成功");
//    }

    @PostMapping("/files_upload")
    public Resp filesUpload(MultipartFile files) {
        UploadInfo upload = uploadService.upload(files, request);
        return RespUtil.success(upload.getId(),"");
    }

    @GetMapping("/resource/{fileId}")
    public void showFiles(HttpServletResponse response, @PathVariable String fileId) throws IOException {
        if ("undefined".equals(fileId) || ParamUtil.isBlank(fileId)) {
            return;
        }
        uploadService.showFiles(response, false, fileId);
    }


}
